JavaScript

A5.u.htmlhighlight Method

Syntax

A5.u.html.highlight(html,search[,prefix,suffix])

A5.u.html.highlight(html,search,replacer)

Arguments

htmlstring

The HTML to preform the highlight on.

searchstringarray

An array of strings or single string to search for in the HTML.

prefixstring

The prefix to put before a search match. If no value is specified then an HTML B tag will be used.

suffixstring

The suffix to put after a search match. If no value is specified then an HTML B tag will be used.

replacerfunction

A function to use to replace each match. This can be used to generate IDs for each highlight.

Returns

htmlstring

The HTML with search results highlighted.

Description

Search highlighting for text in and HTML string.

Discussion

This method will take an arbitrary HTML string, and place prefix and suffix HTML around matches that are not in HTML tags.

Example

// highlight with an I tag
var html = '<a title="hello world">hello world</a>';
html = A5.u.html.highlight(html,['hello','world'],'<i>','</i>');
// html = '<a title="hello world"><i>hello</i> <i>world</i></a>'
// reset and highlight with an I tag with a dynamic ID
html = '<a title="hello world">hello world</a>';
var count = 0;
A5.u.html.highlight(html,['hello','world'],function(search,match,offset,html){
	count++;
	return '<i id="match.'+count+'">'+match+'</i>';
});
// html = '<a title="hello world"><i id="match.1">hello</i> <i id="match.2">world</i></a>'